package com.ruoyi.swla2.util;

import liquibase.pro.packaged.S;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author TsingYu
 * @version 1.0
 * @date 2025/6/25 10:46
 */
public class DTSUtils {
    private static final  String  ACCESS_KEY_ID = "WAM-江苏德高";
    private static final String  SECRET_KEY = "be68e99c8c3838eb309c470907967509";
    private static final  String  HMAC_SHA1 = "HmacSHA1";


    public  static Map<String,String> getSignature() {
        Map<String,String> result = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        String signature = null;
        try {
            signature = getSignature((ACCESS_KEY_ID + "\n" + timestamp).getBytes(), SECRET_KEY.getBytes());
            result.put("timestamp",String.valueOf(timestamp));
            result.put("signature",signature);
            result.put("accessKeyId",ACCESS_KEY_ID);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    private static String getSignature(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {
        SecretKeySpec signingKey = new SecretKeySpec(key, HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data);
        return org.apache.commons.codec.binary.Base64.encodeBase64String(rawHmac);
    }

    public static void main(String[] args) {
       Map<String, String>  map =  getSignature();
        map.entrySet().stream()
                .forEach(entry -> System.out.println(entry.getKey() + " => " + entry.getValue()));
    }

}
